package com.fei.controller;

import com.fei.bean.*;
import com.fei.mapper.SingerMapper;
import com.fei.service.AttentionService;
import com.fei.service.MusicService;
import com.fei.service.SingerService;
import com.fei.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SingerController {

//    全局变量，用于添加或减少粉丝数
//    第一个为歌手id，第二个为1或者-1，代表是添加还是删除粉丝数
    private Map<Integer,Integer> map = new HashMap<>();

    @Autowired
    MusicService musicService;
    @Autowired
    VideoService videoService;
    @Autowired
    SingerService singerService;
    @Autowired
    AttentionService attentionService;

    @RequestMapping("/user/ajax/singer")
    public String singer(String name, Model model, HttpSession session){
        SingerBean singerBean = singerService.selectOneSingerNameAllService(name);
        List<MusicBean> musicBeanList = musicService.selectOneSingerIdAllService(singerBean.getId());
        List<VideoBean> videoBeanList = videoService.selectOneSignerIdAllVideoService(singerBean.getId());
        UserBean userBean = (UserBean)session.getAttribute("user");
        AttentionBean attentionBean = new AttentionBean();
        attentionBean.setPhone(userBean.getPhone());
        attentionBean.setSingerId(singerBean.getId());
        Boolean flag = attentionService.selectOnePhoneAndSingerIdService(attentionBean);
        model.addAttribute("attentionBeanFlag",flag);
        model.addAttribute("videoBeanList",videoBeanList);
        model.addAttribute("singerBean",singerBean);
        model.addAttribute("musicBeanList",musicBeanList);
        return "profile::profile";
    }

//    关注和取消关注该歌手
    @RequestMapping("/user/ajax/singerFansUpdate")
    @ResponseBody
    public String singerFansUpdate(Integer singer_id,HttpSession session){
        UserBean user = (UserBean)session.getAttribute("user");
        AttentionBean attentionBean = new AttentionBean();
        attentionBean.setPhone(user.getPhone());
        attentionBean.setSingerId(singer_id);
        Boolean flag = attentionService.selectOnePhoneAndSingerIdService(attentionBean);
        if (!flag){
            singerService.updatAddSingerFansService(singer_id);
            attentionService.insertUserAttentionService(attentionBean);
        }else {
            singerService.updatDeleteSingerFansService(singer_id);
            attentionService.deleteUserAttentionService(attentionBean);
        }
        return "";
    }


}
